package pl.register.android.activities.students.logic;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import com.google.inject.Singleton;
import pl.register.android.activities.profile.StudentProfileActivity;
import pl.register.android.database.exception.NotFoundCourseException;
import pl.register.android.activities.students.ui.StudentsView;
import pl.register.android.course.Course;
import pl.register.android.database.DatabaseAdapter;
import pl.register.android.database.dao.*;
import pl.register.android.group.StudentsGroup;
import pl.register.android.preferences.ResotorePreferences;
import pl.register.android.storage.external.CourseRegisterSDCardManager;
import pl.register.android.student.Student;
import pl.register.android.year.Year;
import pl.register.android.year.YearType;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Mateusz Strzelecki
 * @since 2013-04-09
 */

@Singleton
public class StudentsPresenter {

    private DatabaseAdapter databaseAdapter;

    private StudentsView studentsView;

    private CourseDao courseDao;

    private YearDao yearDao;

    private StudentGroupDao groupDao;

    private StudentLessonDao lessonDao;

    private StudentsDao studentsDao;


    private Course displayCourse;

    private Year displayYear;

    private Map<YearType, Year> yearMap;

    public StudentsPresenter(DatabaseAdapter databaseAdapter, StudentsView studentsView) {
        this.databaseAdapter = databaseAdapter;
        this.studentsView = studentsView;
        this.courseDao = databaseAdapter.getCourseDao();
        this.yearDao = databaseAdapter.getStudentYearDao();
        this.groupDao = databaseAdapter.getStudentGroupDao();
        this.lessonDao = databaseAdapter.getStudentLessonDao();
        this.studentsDao = databaseAdapter.getStudentDao();
        this.yearMap = new HashMap<YearType, Year>();
    }

    public void initWith(Course course, Map<YearType, Year> yearMap) {
        this.displayCourse = course;
        this.yearMap = yearMap;
    }

    public void populateViews(YearType selectedYearType) {
        displayYear = yearMap.get(selectedYearType);
        if (displayCourse != null) {
            studentsView.populateCourse("" + displayCourse.getStartYear(), "" + displayCourse.getEndYear());
            studentsView.populateYears(displayCourse.getYears());
        }
        if (displayYear != null) {
            studentsView.refreshTitle(displayYear);
            studentsView.populateStudents(displayYear.getStudents());
        }
    }

    public void rebuildForNextCourse(Context context) throws NotFoundCourseException {
        Course nextCourse = new Course(displayCourse.getStartYear() + 1, displayCourse.getEndYear() + 1);
        courseDao.findCourseFor(nextCourse.getStartYear(), nextCourse.getEndYear());
        new AsyncStudentsLoader(context, studentsView, this, nextCourse).execute();
    }

    public void rebuildForPreviousCourse(Context context) throws NotFoundCourseException {
        Course previousCourse = new Course(displayCourse.getStartYear() - 1, displayCourse.getEndYear() - 1);
        courseDao.findCourseFor(previousCourse.getStartYear(), previousCourse.getEndYear());
        new AsyncStudentsLoader(context, studentsView, this, previousCourse).execute();
    }

    public void goToProfile(Activity activity, Student student) {
        Intent intentProfile = new Intent(activity, StudentProfileActivity.class);
        intentProfile.putExtra(StudentProfileActivity.EXTRA_YEAR_ID, displayYear.getId());
        intentProfile.putExtra(StudentProfileActivity.EXTRA_STUDENT_ID, student);
        intentProfile.putExtra(StudentProfileActivity.EXTRA_COURSE_ID, displayCourse.getId());
        activity.startActivity(intentProfile);
    }

    public void saveStudent(Student student) {
        Year displayYear = getDisplayYear();
        if (student.isPersist()) {
            studentsDao.save(student, displayYear, displayCourse);
            studentsView.refresh();
        } else {
            studentsDao.save(student, displayYear, displayCourse);
            lessonDao.insertLessonFor(student, displayYear.getProgram());
            displayYear.addStudent(student);
            CourseRegisterSDCardManager.createStudentGalleryFolder(student);
            studentsView.refresh();
        }
    }

    public void deleteStudent(Student student) {
        StudentsGroup group = groupDao.findGroupsBy(student, displayYear);
        if (group != null) {
            groupDao.deleteStudentFrom(student, group);
        }
        lessonDao.deleteLessonsFrom(student, displayYear.getProgram());
        studentsDao.deleteFrom(student, displayYear);
        displayYear.deleteStudent(student);
        studentsView.refresh();
    }

    public void changeSubNameYear(String subName) {
        displayYear.setSubName(subName);
        yearDao.updateYear(displayYear);
        studentsView.refreshTitle(displayYear);
    }

    public void changeYearType(Context context, YearType yearType) {
        displayYear = yearMap.get(yearType);
        if (displayYear != null) {
            studentsView.refreshTitle(displayYear);
            studentsView.populateStudents(displayYear.getStudents());
        }
        ResotorePreferences.saveYearType(context, yearType);
    }

    public Year getDisplayYear() {
        return displayYear;
    }

    public DatabaseAdapter getDatabaseAdapter() {
        return databaseAdapter;
    }

    public boolean contentIsLoaded() {
        return displayCourse != null && displayYear != null;
    }
}